package com.phone.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.common.pojo.R;
import com.phone.client.mapper.CoupMapper;
import com.phone.client.poji.entry.TbCoupon;
import com.phone.client.poji.vo.CoupVo;
import com.phone.client.service.CoupService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: clms-seventh
 * @description: CoupService的实现
 * @author: 杨佳琪
 * @create: 2021-07-24 16:15
 **/
@ApiModel(value="优惠券CoupService的实现", description="优惠券CoupService的实现")
@Service
public class
CoupServiceImpl implements CoupService {
    @Resource
    private CoupMapper coupMapper;

    @ApiModelProperty("分页查询")
    @Override
    public R initCoupTable(Integer currentPage, Integer pageSize, CoupVo coupVo) {
        Page<TbCoupon> page=new Page<>(currentPage,pageSize);
        QueryWrapper<TbCoupon> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(coupVo.getCouname())){
            wrapper.like("couname",coupVo.getCouname());
        }

        IPage<TbCoupon> page1=coupMapper.initCoupTable(page,wrapper);
        return R.ok().put("records",page1.getRecords()).put("total",page1.getTotal());
    }

    @ApiModelProperty("删除")
    @Override
    public R deleteCoup(Integer id) {
        int i=coupMapper.deleteById(id);
        return R.ok();
    }

    @ApiModelProperty("修改")
    @Override
    public R updateCoup(TbCoupon tbCoupon) {
        coupMapper.updateById(tbCoupon);
        return R.ok();
    }

    @ApiModelProperty("新增")
    @Override
    public R insertCoup(TbCoupon tbCoupon) {
        coupMapper.insert(tbCoupon);
        return R.ok();
    }

    @ApiModelProperty("修改的回显")
    @Override
    public R findCoupById(Integer id) {
        TbCoupon coupon=coupMapper.selectById(id);
        return R.ok().put("coupon",coupon);
    }

    @Override
    public R selCode(TbCoupon tbCoupon) {
        List<TbCoupon> list=coupMapper.selectList(null);
        int result=0;
        for (TbCoupon u:list) {
            if (tbCoupon.getId()!=null){
                if (!u.getId().equals(tbCoupon.getId())){
                    /*System.out.println(u.getYcode());
                    System.out.println(tbCoupon.getYcode());*/
                    if (u.getYcode().equals(tbCoupon.getYcode())){
                        result=1;
                        break;
                    }
                }
            }else {
                if (u.getYcode().equals(tbCoupon.getYcode())){
                    result=1;
                    break;
                }
            }
        }
        return R.ok().put("code",result);
    }
}
